package com.tcp2.util;

import com.yaxon.vndp.geo.ad.AdminDivisionUtils;
import com.yaxon.vndp.geo.ad.Area;

/**
 * Author: 程行荣
 * Time: 2014-01-17 16:11
 * Copyright (C) 2014 Xiamen Yaxon Networks CO.,LTD.
 */
public class GeoUtils {
    private static AdminDivisionUtils adu;

    static {
        try {
            adu = new AdminDivisionUtils();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static String getPosition(Integer lon, Integer lat) {
        Double _lon = null;
        Double _lat = null;
        if (lon != null) {
            _lon = lon/1000000.0;
        }

        if (lat != null) {
            _lat = lat/1000000.0;
        }

        return getPosition(_lon, _lat);
    }

    public static String getPosition(Double lon, Double lat) {
        if (lon == null || lat == null ||
                lon < -180 || lon > 180 || lat < -90 || lat > 90) {
            return "经纬度无效";
        }

        Area area = adu.getAreaByLonLat(lon, lat);
        StringBuilder sb = new StringBuilder();
        if (area == null) {
            sb.append("未知的位置[").append(lon).append(",").append(lat).append("]");
        } else {
            sb.append(area.getFullName());
        }
        return sb.toString();
    }

    public static String getAlarmName(Integer v_Alarm){
        String strDesp="";
        Long urgentAlarm = 0x1L;
        Long overspeedAlarm=0x2L;
        Long tiredAlarm=0x4L;
        Long warning = 0x8L;
        Long gnssFailure=0x10L;
        Long gnssAntennaOff=0x20L;
        Long gnssAntennaShort=0x40L;
        Long powerV = 0x80L;
        Long powerOff = 0x100L;
        Long lcdFailure=0x200L;
        Long ttsFailure=0x400L;
        Long camaFailure=0x800L;
        Long icFailure=0x1000L;
        Long overSpeedPre = 0x2000L;
        Long tiredPre = 0x4000L;

        Long driveOvertime=0x40000L;
        Long stopOver = 0x80000L;

        Long inOutArea = 0x100000L;
        Long inOutLine = 0x200000L;
        Long runOvertime = 0x400000L;


        Long overLine = 0x800000L;
        Long vssFailure=0x1000000L;
        Long oilExcep=  0x2000000L;
        Long stolen =   0x4000000L;
        Long startAlarm = 0x8000000L;

        Long moveAlarm =    0x10000000L;
        Long crashAlarm =   0x20000000L;
        Long rollover =     0x40000000L;
        Long illeagalOpen = 0x80000000L;
        if(null == v_Alarm)
        {
            return "";
        }

        if ((v_Alarm&urgentAlarm)==urgentAlarm){
            strDesp=strDesp+" 紧急报警";
        }
        if ((v_Alarm&overspeedAlarm)==overspeedAlarm){
            strDesp=strDesp+" 超速报警";
        }
        if ((v_Alarm&tiredAlarm)==tiredAlarm){
            strDesp=strDesp+" 疲劳驾驶报警";
        }

        if((v_Alarm&warning)==warning){
            strDesp=strDesp+" 危险预警";
        }

        if ((v_Alarm&gnssFailure)==gnssFailure){
            strDesp=strDesp+" GNSS模块故障";
        }
        if ((v_Alarm&gnssAntennaOff)==gnssAntennaOff){
            strDesp=strDesp+" GNSS天线未接或开路";
        }

        if ((v_Alarm&gnssAntennaShort)==gnssAntennaShort){
            strDesp=strDesp+" GNSS天线短路";
        }
        if ((v_Alarm&powerV)==powerV){
            strDesp=strDesp+" 终端主电源欠压";
        }
        if ((v_Alarm&powerOff)==powerOff){
            strDesp=strDesp+" 终端主电源掉电";
        }
        if ((v_Alarm&lcdFailure)==lcdFailure){
            strDesp=strDesp+" LCD或显示器故障";
        }
        if ((v_Alarm&ttsFailure)==ttsFailure){
            strDesp=strDesp+" TTS模块故障";
        } if ((v_Alarm&camaFailure)==camaFailure){
            strDesp=strDesp+" 摄像头故障";
        } if ((v_Alarm&icFailure)==icFailure){
            strDesp=strDesp+" IC卡故障";
        } if ((v_Alarm&overSpeedPre)==overSpeedPre){
            strDesp=strDesp+" 超速驾驶预警";
        } if ((v_Alarm&tiredPre)==tiredPre){
            strDesp=strDesp+" 疲劳驾驶预警";
        } if ((v_Alarm&driveOvertime)==driveOvertime){
            strDesp=strDesp+" 单日累积驾驶超时";
        }
        if ((v_Alarm&stopOver)==stopOver){
            strDesp=strDesp+" 超时停车报警";
        }

        if((v_Alarm&inOutArea)==inOutArea){
            strDesp=strDesp+" 进出区域报警";
        }
        if((v_Alarm&inOutLine)==inOutLine){
            strDesp=strDesp+" 进出线路报警";
        }
        if((v_Alarm&runOvertime)==runOvertime){
            strDesp=strDesp+" 超时行驶报警";
        }

        if((v_Alarm&overLine)==overLine){
            strDesp=strDesp+" 偏移线路报警";
        }

        if((v_Alarm&vssFailure)==vssFailure){
            strDesp=strDesp+" VSS故障";
        }

        if((v_Alarm&oilExcep)==oilExcep){
            strDesp=strDesp+" 油量异常报警";
        }

        if((v_Alarm&stolen)==stolen){
            strDesp=strDesp+" 车辆被盗报警";
        }


        if((v_Alarm&startAlarm)==startAlarm){
            strDesp=strDesp+" 车辆非法点火报警";
        }
        if((v_Alarm&moveAlarm)==moveAlarm){
            strDesp=strDesp+" 车辆非法位移报警";
        }


        if((v_Alarm&crashAlarm)==crashAlarm){
            strDesp=strDesp+" 碰撞报警";
        }


        if((v_Alarm&rollover)==rollover){
            strDesp=strDesp+" 侧翻报警";
        }


        if((v_Alarm&illeagalOpen)==illeagalOpen){
            strDesp=strDesp+" 非法开车门报警";
        }
        return  strDesp;
    }

}
